home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / spirals9.zip / SPIRALS9.CPP < prev    next >
C/C++ Source or Header  |  1991-12-18  |  5KB  |  147 lines

  1. /*
  2.     SPIRALS9    12/18/91    v1.0    gjpateman
  3.  
  4.     SPIRALS8 reads some of it's parameters from GJP.INI, then choses
  5.     a random rectangle on the Windows 3.0 screen.  The rectangle is
  6.     duplicated "arms" times & spiraled outward from the center.
  7.     When an arm goes offscreen, it votes to exit the program (how
  8.     democratic!). When this vote becomes unanimous among the arms,
  9.     the program exits.  While an arm is offscreen, the code to draw
  10.     it is skipped, thereby speeding up the arms which are visible.
  11.  
  12.     Future enhancements:
  13.         Draw or don't draw border (in INI)
  14.         Use mouse/keyboard to abort
  15.         All parameters from GJP.INI
  16.         Window to set parameters
  17.         Use mouse to designate area to spiral
  18.         Show a message box before aborting if illegal .INI values.
  19.  
  20.     Sample GJP.INI:
  21.  
  22. */
  23.  
  24. #include <windows.h>
  25. #include <time.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28.  
  29. #define MODES    16
  30.  
  31. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  32.             LPSTR lpszCmdParam, int nCmdShow)
  33. {
  34.   HDC        hdc, hdcmem;                    // DC for screen & RAM
  35.   HBITMAP    bitmap;                // holds copied image
  36.   POINT        src, dest;            // source/dest locations
  37.   int        scrnwidth, scrnheight;
  38.   char        inifile[] = "GJP.INI",
  39.         app[]     = "SPIRALS";
  40.   BOOL        drawborder = 1;
  41.   WORD        arm, arms,
  42.         modeindex,
  43.         votes_to_quit,
  44.         minwidth,  maxwidth,
  45.         minheight, maxheight,
  46.         srcwidth,  srcheight;
  47.   DWORD        bitbltmodes[MODES] =
  48.         {
  49.           0, SRCCOPY, SRCPAINT, SRCAND, SRCINVERT, SRCERASE,
  50.           NOTSRCCOPY, NOTSRCERASE, MERGECOPY, MERGEPAINT,
  51.           PATCOPY, PATPAINT, PATINVERT, DSTINVERT, BLACKNESS,
  52.           WHITENESS
  53.         };
  54.   double        r, rstep, rstepfact,
  55.         ang, angstep,
  56.         destwidth, destheight,
  57.         widthstep, heightstep,
  58.         offsetstep;
  59.  
  60.   randomize();
  61.  
  62.   // initialize variables from .INI file
  63.   arms       = GetPrivateProfileInt(app, "arms",        arms,       inifile);
  64.   minwidth   = GetPrivateProfileInt(app, "minwidth",    minwidth,   inifile);
  65.   minheight  = GetPrivateProfileInt(app, "minheight",   minheight,  inifile);
  66.   maxwidth   = GetPrivateProfileInt(app, "maxwidth",    maxwidth,   inifile);
  67.   maxheight  = GetPrivateProfileInt(app, "maxheight",   maxheight,  inifile);
  68.   modeindex  = GetPrivateProfileInt(app, "modeindex",   modeindex,  inifile);
  69.   drawborder = GetPrivateProfileInt(app, "drawborder",  drawborder, inifile);
  70.  
  71.   if(arms      < 1)        arms      = 3;
  72.   if(minwidth  < 1)        minwidth  = 5;
  73.   if(minheight < 1)        minheight = 5;
  74.   if(maxheight < minheight)    maxheight = minheight * 2;
  75.   if(maxwidth  < minwidth)    maxwidth  = minwidth * 2;
  76.   if(modeindex < 1)        modeindex = 1;
  77.   if(modeindex > MODES)        modeindex = 1;
  78.  
  79.   // initialize all other variables
  80.   hdc        = CreateDC ("DISPLAY", NULL, NULL, NULL);
  81.   hdcmem    = CreateCompatibleDC(hdc);
  82.   scrnwidth    = GetSystemMetrics(SM_CXSCREEN);
  83.   scrnheight    = GetSystemMetrics(SM_CYSCREEN);
  84.   srcwidth    = minwidth  + random(maxwidth  - minwidth);
  85.   srcheight    = minheight + random(maxheight - minheight);
  86.   destwidth    = (double) srcwidth;
  87.   destheight    = (double) srcheight;
  88.   widthstep    = 1.03;
  89.   heightstep    = 1.03;
  90.   src.x        = random(scrnwidth - srcwidth);
  91.   src.y        = random(scrnheight - srcheight);
  92.   offsetstep    = (M_PI + M_PI) / (double)arms;    // space between arms
  93.   bitmap    = CreateCompatibleBitmap(hdc, srcwidth, srcheight);
  94.   rstep        = 1.0;
  95.   rstepfact    = 1.03;
  96.   ang        = 0.0;
  97.   angstep    = 0.04;
  98.  
  99.   if(drawborder == 1)
  100.   {
  101.     MoveTo(hdc, src.x, src.y);
  102.     LineTo(hdc, src.x + srcwidth - 1, src.y);
  103.     LineTo(hdc, src.x + srcwidth - 1, src.y + srcheight - 1);
  104.     LineTo(hdc, src.x, src.y + srcheight - 1);
  105.     LineTo(hdc, src.x, src.y);
  106.   }
  107.  
  108.   // save area to be "spiraled" into RAM DC
  109.   SelectObject(hdcmem, bitmap);
  110.   BitBlt(hdcmem, 0, 0, srcwidth, srcheight, hdc, src.x, src.y, SRCCOPY);
  111.  
  112.   do    // Spiral until unanimous vote to quit
  113.   {
  114.     votes_to_quit = 0;    // new vote each time
  115.     for(arm = 0; arm < arms; ++arm)
  116.     {
  117.       // Calculate new position
  118.       dest.x = src.x + (int)(r * sin(ang + (arm * offsetstep)));
  119.       dest.y = src.y + (int)(r * cos(ang + (arm * offsetstep)));
  120.  
  121.       // Draw rect ONLY if new position is completely/partially on-screen
  122.       if((dest.x + destwidth  > 0) && (dest.x < scrnwidth) &&
  123.      (dest.y + destheight > 0) && (dest.y < scrnheight))
  124.  
  125.     StretchBlt( hdc,
  126.             dest.x, dest.y,
  127.             (int)destwidth, (int)destheight,
  128.             hdcmem,
  129.             0, 0,
  130.             srcwidth, srcheight,
  131.             bitbltmodes[ modeindex ] );
  132.       else
  133.     ++votes_to_quit;
  134.     }
  135.     destheight *= heightstep;
  136.     destwidth  *= widthstep;
  137.     r          += rstep;
  138.     rstep      *= rstepfact;
  139.     ang        += angstep;
  140.   } while ( votes_to_quit != arms );    // is it unanimous yet?
  141.  
  142.   DeleteDC(hdc);
  143.   DeleteDC(hdcmem);
  144.   return 0;
  145. }
  146.  
  147.